//
//  String+Regular.swift
//  SwiftLNGDemo
//
//  Created by luohanguo on 2022/12/13.
//

import Foundation
import UIKit

public extension String {
    func isNumberRegular() -> Bool {
        let regular = "^[0-9]*$"
        let regexPassword = NSPredicate(format: "SELF MATCHES %@",regular)
        return regexPassword.evaluate(with: self)
    }
    
    func isPhoneRegular() -> Bool {
        let regular = "^(((\\+\\d{2}-)?0\\d{2,3}-\\d{7,8})|((\\+\\d{2}-)?(\\d{2,3}-)?([1][3,4,5,7,8][0-9]\\d{8})))$"
        let regexPassword = NSPredicate(format: "SELF MATCHES %@",regular)
        return regexPassword.evaluate(with: self)
    }
    
    /// 6~15位, (数字/小写字母/大写字母/特殊字符)必须有其中3种
    func isPasswordRegular() -> Bool {
        let regular = "^(?![a-zA-Z]+$)(?![A-Z0-9]+$)(?![A-Z\\W_!@#$%^&*`~()-+=]+$)(?![a-z0-9]+$)(?![a-z\\W_!@#$%^&*`~()-+=]+$)(?![0-9\\W_!@#$%^&*`~()-+=]+$)[a-zA-Z0-9\\W_!@#$%^&*`~()-+=]{6,15}$"
        let regexPassword = NSPredicate(format: "SELF MATCHES %@",regular)
        return regexPassword.evaluate(with: self)
    }
    
    ///端口号 数值范围在[0,65535]
    func isPortRegular() -> Bool {
        let regular = "^[0-9]|[1-9][0-9][0-9]{0,2}|[1-5][0-9]{4}|6([0-4][0-9]{3}|5([0-4][0-9]{2}|5([0-2][0-9]|3[0-5])))$"
        let regex = NSPredicate(format: "SELF MATCHES %@",regular)
        return regex.evaluate(with: self)
    }
}
